import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test_demo/pages/home/components/RecommendInfo.dart';
import 'package:flutter_test_demo/pages/set/UserSetting.dart';
import 'package:flutter_test_demo/pages/upload/UploadFile.dart';
import 'package:flutter_test_demo/static/colors/MyColors.dart';
import 'package:photo_album_manager/photo_album_manager.dart';
import 'package:toast/toast.dart';

// 我的

class MyPage extends StatefulWidget {
  MyPage({Key key}) : super(key: key);

  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的"),
        backgroundColor: MyColor.titleColor,
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          // 距离上边距
          HeightSizeContainter(conHeight: 40.0,),
          // 用户头像信息
          UserAvator(),

          HeightSizeContainter(conHeight: 20.0,),
          //  设置选项
          UserSet(),
        
          Expanded(child: SizedBox()),

          // 退出
          ConstrainedBox(
            constraints: BoxConstraints.tightFor(width: double.infinity),
            child: Padding(
              padding: EdgeInsets.all(20.0),
              child: RaisedButton(
                color: MyColor.titleColor,
                textColor: Colors.white,
                onPressed: (){
                  _cuperAlert(context);
                },
                child: Text("退出"),
              ),
            ),
          ),

        ],
      ),
      
    );
  }
}

class HeightSizeContainter extends StatelessWidget {
  final double conHeight ;
  final Color conColor;
  const HeightSizeContainter({
    Key key,
    @required this.conHeight,
    this.conColor = const Color(0xFFEEEEEE)
    }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: conHeight,
      decoration: BoxDecoration(
        color: conColor
      ),
    );
  }
}
// 用户头像信息
class UserAvator extends StatelessWidget {
  const UserAvator({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Map userInfo = {
      "title":"小小的可爱",
      "image": 'images/avator.jpg',
      "age":'20',
      "mm": "后山红花红火火",
      "des":"内心的狂人，才懂得生活的含义"
    };
    return  Container(
      height: 100.0,
      padding: EdgeInsets.symmetric(
        vertical: 10.0,
        horizontal: 15.0
      ),
      child: GestureDetector(
        onTap: (){
          Navigator.push(
            context, 
            MaterialPageRoute(builder: (context) => RecommendInfo(
              reInfo: userInfo
            )
            )
          );
        },
        child: Row(
          children: <Widget>[
            // 头像
            DecoratedBox(
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(35.0),
                image:DecorationImage(
                  image: AssetImage(
                    userInfo["image"]
                  )
                )
              ),
              child: ConstrainedBox(
                constraints: BoxConstraints.tightFor(
                  height: 70.0,
                  width: 70.0
                ),
              )
            ),
            SizedBox(width: 20.0,),
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  userInfo["title"],
                  style: TextStyle(
                    letterSpacing: 2.0,
                    fontSize: 19.0,
                    fontWeight: FontWeight.w700
                  ),
                ),
                Text(
                  userInfo["des"]
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

class UserSet extends StatelessWidget {
  const UserSet({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: ListTile.divideTiles(
        context: context,
        tiles: [
          ListTile(
            title: Text("本地视频"),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: (){
              jumpLocalResource(context,"video");
            },
          ),
          ListTile(
            title: Text("本地相册"),
            trailing: Icon(Icons.keyboard_arrow_right),
             onTap: (){
              jumpLocalResource(context,"photo");
            },
          ),
          ListTile(
            title: Text("设置"),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: (){
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => UserSeting() )
              );
            },
          )
        ]
      ).toList()
    );
  }
}

void jumpLocalResource(BuildContext context,jumpName) async{
  if(jumpName == "video" || jumpName == "photo"){
    //  检查权限
    PermissionStatus status = await PhotoAlbumManager.checkPermissions();
    //  用户同意后跳转上传页面
    if(status == PermissionStatus.granted){
      Navigator.push(
        context, 
        MaterialPageRoute(builder: (context) => UpLoadFile(resoureType: jumpName),)
      );
    }else{
      Toast.show("获取权限失败，请打开相册权限", context);
    }
  }
}

// 退出提示框
void _cuperAlert(BuildContext context){
  showCupertinoDialog(
    context: context, 
    builder: (context){
      return CupertinoAlertDialog(
        title: Text("退出账号"),
        content: Text("确认退出当前账号？"),
        actions: <Widget>[
           CupertinoDialogAction(
            child: Text('取消'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
           CupertinoDialogAction(
            child: Text('确认'),
            onPressed: () {
              Navigator.of(context)..pop()..pushReplacementNamed("/home");
            },
          ),
        ],
      );
    }
  );
}